<?php

namespace App\Http\Controllers\Home;
 
use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AddressController extends Controller
{

  public function getIndex()
  {
    //返回 地址首页
 // var_dump(session()->get("username") ) ;die;    
    return view('home/car/address');

  } 
  //添加地址
  public function postInsert(Request $request){
   $uid =  session()->get('username')->id;//获取用户的id 
    
    $res =$request->except('_token');//选出 有用的数据;
    
    $pro =$res['consignee_province'];//从数据库选出省
    $pid=$pro; 
    $province = DB::table('project_address_list')->where('pid','=',$pro)->first(); 
    $pros= $province->province; 

    $ci=$res['consignee_city'];var_dump($ci);   //选出市
    $cid=$ci;
    $city= DB::table('project_address_list')->where('uid','=',$pro.'-'.$ci)->first();
    $cis=$city->city; 
    
    // //选出的省和市插入有用的数据中
    $res['consignee_province'] =$pros;
    $res['consignee_city']=$cis;
    $res['uid']=$uid;
    $res['pid']=$pid;
    $res['cid']=$cid;

    //插入地址表
    $row =DB::table('project_address')->insert($res);  //

    //获取插入的id存入session中
   $pro_id =DB::table("project_address")->insertGetId($res);
    
    $res['id']=$pro_id;
    // var_dump($res);
    //将数据存入session()中     
   session()->push('address',$res);
      
    if($row){
            return back()->with('success','添加地址成功');
        } else{
            return back()->with('error','添加地址失败');
          }
        
    }

   //执行删除地址
   public function getDel($id)
   {
      // 获取地址   $id 为session中的id
        $res=session()->get("address");
     // 
     // 从数据库中删除相对应的值 var_dump($mid);
        $mid = $res[$id]['id']; 
       
        DB::table('project_address')->where('id','=',$mid)->delete();
       
         unset($res[$id]);
        session()->put('address',$res);
      
      return back()->with('success','删除成功');
    }
  
    //执行编辑地址
    public function  getEdite()
    {
        $id = $_GET['id'];
        //从数据库中选出对应的地址
        $info = DB::table('project_address')->where('id','=',$id)->first();
        
        echo json_encode($info);

    }

    //进行地址的更新
    public function postUpdate(Request $request)
    {
      //用户的uid
     $uid =  session()->get('username')->id;//获取用户的id 

        //获取id
        $id =$request->input('id');   //获取id var_dump($id);

        //获取mid session中对应的id
        $mid =$request->input('mid'); //var_dump($mid);

       $res =$request->except('_token','id','mid');   //var_dump($res);die;//获取有效字段
     
       //将省 市 0 1 数字转换为对应的值
       
         $pro =isset($res['consignee_province'])?$res['consignee_province']:'0';   
         $pid=$pro; 
        $province = DB::table('project_address_list')->where('pid','=',$pro)->first(); 
        $pros= $province->province; //选出对应的省
      
        $ci=isset($res['consignee_city'])?$res['consignee_city']:'0';  
        $cid=$ci;
        $city= DB::table('project_address_list')->where('uid','=',$pro.'-'.$ci)->first();
        $cis=$city->city;   //var_dump($cis); //选出市

          // //选出的省和市插入有用的数据中
          $res['consignee_province'] =$pros;
          $res['consignee_city']=$cis;
          $res['uid']=$uid;
          $res['pid']=$pid;
          $res['cid']=$cid;
        $row = DB::table('project_address')->where('id','=',$id)->update($res); //更新数据
               

        $res['id']=$id; var_dump($res);
      //获取session中对应的值 

        $sess=session()->get("address"); 
        //将更新的数据写入session
           $sess[$mid] =$res;
          
          session()->put('address',$sess);
          
       if($row){
            return back()->with('success','更新地址成功');
        } else{
            return back()->with('error','更新地址失败');}

    }


    //执行生成订单
    public function getOrder()
    {   
        //获取用户的id
         $uid =  session()->get('username')->id;//获取用户的id   

         $orderprice =$_GET['pri'];///获取总价/var_dump($pri);
        
         $addre =$_GET['addre']; //获取地址//var_dump($addre);

         $ordernum=time().rand('10000','99999');//var_dump($ordernum);//生成订单号码
        
        $goods=session()->get('shoplist');      // var_dump($goods);   //获取商品//;
        $ordertime=date("Y-m-d H:i:s",time()); //var_dump($ordertime);
        $res=array();
       foreach ($goods as $k => $v) {
              
            $gid =$v->id;  //获取商品的id var_dump($gid);
            $goodspic =$v->goodspic; //商品的图片
            $goodsname=$v->goodsname;//商品的名字
            $num =$v->m;  //商品的购买数量
            $goodsprice =$v->goodsprice; //var_dump($orderprice); //商品的购买数量

       }

        $res['uid']=$uid;$res['ordernum']=$ordernum;
        $res['gid']=$gid; $res['num']=$num;
        $res['orderprice']=$orderprice;
        $res['goodsname']=$goodsname;$res['goodspic']=$goodspic;
        $res['addre']=$addre;
        $res['ordertime']=$ordertime;
        

        //插入订单表
        $row= DB::table('project_order')->insert($res);
      var_dump($row);die;
        if($row){
            return '1';
        } else{
          return '0';
        }
   
    }

    //跳转生成订单的页面

    public function getOpp()
    { 

      return view('/home/car/order');
    }
}

